/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MapEditor;

import YottaTron.Runner;
import YottaTron.Sprites.ObstacleSprite;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JPanel;

public class MapObjectsMenu extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

    int sirka, vyska;
    ArrayList<ObstacleSprite> obstacles;
    MainFrame mainFrame;
    public int tileWidth, tileHeight;

    public MapObjectsMenu(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        this.sirka = 150;
        this.vyska = 1800;

        // velikost tiles urcuje hrubost rozmisteni predmetu v mape
        tileWidth = 10;
        tileHeight = 10;

        obstacles = new ArrayList();

        // do id se ulozi index grafiky v runner.staticGraphics.. jine vyuziti zde id nema
        obstacles.add(new ObstacleSprite(1, 1, Runner.GSP_ROCK, mainFrame.graphics[Runner.GSP_ROCK]));
        obstacles.add(new ObstacleSprite(1, 25, Runner.GSP_TREE0, mainFrame.graphics[Runner.GSP_TREE0]));
        obstacles.add(new ObstacleSprite(1, 50, Runner.GSP_TREE1, mainFrame.graphics[Runner.GSP_TREE1]));
        obstacles.add(new ObstacleSprite(1, 75, Runner.GSP_TREE2, mainFrame.graphics[Runner.GSP_TREE2]));
        obstacles.add(new ObstacleSprite(1, 100, Runner.GSP_TREE3, mainFrame.graphics[Runner.GSP_TREE3]));
        obstacles.add(new ObstacleSprite(1, 125, Runner.GSP_TREE4, mainFrame.graphics[Runner.GSP_TREE4]));

        this.addMouseMotionListener(this);
        this.addKeyListener(this);
        this.addMouseListener(this);
        this.setPreferredSize(new Dimension(sirka, vyska));
    }

    public void mouseClicked(MouseEvent e) {
    }

    // umisteni objektu
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("pressed");
        // oznaceni aktivniho herniho prvku
        for (Iterator<ObstacleSprite> it = obstacles.iterator(); it.hasNext();) {
            ObstacleSprite obstacle = it.next();
            if (((x > obstacle.x) && (x < obstacle.x + obstacle.width)) &&
                        ((y > obstacle.y) && (y < obstacle.y + obstacle.height))) {
                    mainFrame.mapPanel.activeObject = new ObstacleSprite(obstacle.x, obstacle.y, obstacle.ID, obstacle.obstacleGraphic);
                }
        }

        // pro oznaceni typu povrchu, puze pomoci ctvercu... k indexu se pricita 1000
         for (int i = 0; i < 5; i++) {
            if ((x > 38) && (x < 58) && (y > i * 30 - 2) && (y < i * 50 - 2)){
                mainFrame.mapPanel.activeObject = new ObstacleSprite(x, y, 1000 + i, mainFrame.graphics[Runner.GSP_ROCK]);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, sirka, vyska);



        for (Iterator it = obstacles.iterator(); it.hasNext();) {
            ObstacleSprite o = (ObstacleSprite) it.next();
            g2.drawImage(o.obstacleGraphic.getImage(0), (int) o.x, (int) o.y, null);
        }

        for (int i = 0; i < 5; i++) {
            g2.setColor(Color.WHITE);
            g2.fillRect(38, i * 30 - 2, 20, 20);
            g2.setColor(Color.GREEN);
            g2.drawChars(Integer.toString(i).toCharArray(), 0, 1, 40, i * 30 + 10);
        }

    }
}